package solutions.leetcode.hard;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/self-crossing/">335. 路径交叉</a>
 */
public class Solution335 extends BaseSolution {

    public boolean isSelfCrossing(int[] distance) {
        for (int i = 0; i < distance.length; i++) {
            boolean case1 = i >= 3 && distance[i - 1] <= distance[i - 3] && distance[i] >= distance[i - 2];
            boolean case2 = i >= 4 && distance[i - 1] == distance[i - 3] && distance[i - 2] <= distance[i] + distance[i - 4];
            boolean case3 = i >= 5 && distance[i - 2] <= distance[i] + distance[i - 4]
                    && distance[i - 3] <= distance[i - 1] + distance[i - 5]
                    && distance[i - 2] >= distance[i - 4] && distance[i - 1] <= distance[i - 3];
            if (case1 || case2 || case3) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Solution335 solution = new Solution335();
        System.out.println(solution.isSelfCrossing(new int[] {
                1,1,2,2,3,3,4,4,10,4,4,3,3,2,2,1,1
        }));
    }
}